import 'package:flutter/material.dart';

import 'ColorUtil.dart';
import 'commonUtil.dart';


class DialogCommon{
  /**
   * 对话框
   * context：上下文
   * tip：提醒内容，
   * successCallBack：成功返回
   * cancelBack：失败返回
   * tipTitle：提示头部信息
   */
  alertDialog(context,  {required String tip,successCallBack, cancelBack,tipTitle='提示'}){
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (context){
      return AlertDialog(
        backgroundColor: Colors.white,
        title: Text(tipTitle,style: const TextStyle(fontSize: 18),),
        content: Text(tip),
        actions: <Widget>[
          ElevatedButton(
            style: ButtonStyle(backgroundColor: WidgetStateProperty.all(Color.fromRGBO(165, 165, 165, 1))),
            child: const Text("取消",style: TextStyle(color: Colors.white),),
            onPressed: () => {
              cancelBack(),
              Navigator.of(context).pop()
            }, //关闭对话框
          ),
          ElevatedButton(
            style: ButtonStyle(backgroundColor: WidgetStateProperty.all(ColorUtil.primaryBgColor)),
            child: const Text("确定",style: TextStyle(color: Colors.white),),
            onPressed: () {
              successCallBack();
              Navigator.of(context).pop(true); //关闭对话框并返回true
            },
          ),
        ],
      );
    });
  }

  /**
   * 底部弹出选项菜单
   * context: 上下文
   * options: 选项列表 [{label: '选项名称'}]
   * onCancel: 取消回调
   * onSelected: 选项选中回调 (index) => void
   */
  showBottomSheetOptions(context, List<Map<String, String>> options, {int ? selectedIndex, Function()? onCancel, Function(int)? onSelected}) {
    showModalBottomSheet<void>(
      context: context,
      backgroundColor: Colors.white,
      builder: (context) => Wrap(
        children: [
          // 动态生成选项列表
          ...options.asMap().entries.map((entry) {
            int index = entry.key;
            var option = entry.value;
            return ListTile(
              title: Center(child: Text(
                  option['label']!,
                  style: TextStyle(
                      color: selectedIndex == index ? Colors.white : null
                  )
              )
              ),
              tileColor: selectedIndex == index ? ColorUtil.primaryBadgeBgColor : null, // 选中时添加背景色
              onTap: () {
                Navigator.pop(context);
                if (onSelected != null) {
                  onSelected(index);
                }
              },
            );
          }).toList(),
          const Divider(),
          ListTile(
            title: const Center(child: Text('取消', style: TextStyle(color: Colors.red))),
            onTap: () {
              Navigator.pop(context);
              if (onCancel != null) {
                onCancel();
              }
            },
          ),
        ],
      ),
    );
  }






}

DialogCommon dialogUtils = DialogCommon();